package com.test.timmy;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;


public class PersonDetails
	extends Activity
	implements Observer
{
	private Person person;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.person_details);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle != null)
		{
			String personID = bundle.getString(Person.TAG_PERSON_ID);
			person = Controller.getInstance().getPeople().get(personID);
			person.addObserver(this);
			draw(person);
		}
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		draw(person);
	}
	
	private void draw(final Person person)
	{
		ImageView myImage = (ImageView) findViewById(R.id.photo);
		myImage.setImageBitmap(person.getPhoto());

		TextView name = (TextView) findViewById(R.id.name);
		name.setText(person.getName());

		TextView department = (TextView) findViewById(R.id.department);
		department.setText(person.getDepartment());

		TextView morale = (TextView) findViewById(R.id.morale);
		morale.setText(String.valueOf(person.getMorale()));

		TextView humour = (TextView) findViewById(R.id.humour);
		humour.setText(String.valueOf(person.getHumour()));
		
		TextView skill = (TextView) findViewById(R.id.skill);
		skill.setText(String.valueOf(person.getSkill()));
		
		final Spinner lifeEvents = (Spinner) findViewById(R.id.spinner1);
		ArrayList<String> eventsArray = new ArrayList<String>(Controller.getInstance().getLifeEvents().keySet());
		ArrayAdapter<String> spinnerArrayAdapter =
				new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, eventsArray);
		lifeEvents.setAdapter(spinnerArrayAdapter);

		Button applyEvent = (Button) findViewById(R.id.button1);
		applyEvent.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String event = (String) lifeEvents.getSelectedItem();
				Intent intent = new Intent(getApplicationContext(), TeamOverview.class);
				intent.putExtra(LifeEvent.TAG_EVENT, event);
				intent.putExtra(Person.TAG_PERSON_ID, person.getUniqueID());
				//for back button support
				PersonDetails.this.startActivity(intent);
			}
		});
	}

	//doesn't work...
	@Override
	public void update(Observable observable, Object data)
	{
		draw((Person)observable);
	}
}
